/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.test2.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.app.entity.OrderGoods2;
import com.thinkgem.jeesite.modules.test2.entity.FarmClass;
import com.thinkgem.jeesite.modules.test2.service.FarmClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.test2.entity.Farm;
import com.thinkgem.jeesite.modules.test2.service.FarmService;

import java.util.ArrayList;
import java.util.List;

/**
 * 养殖场管理Controller
 * @author dby
 * @version 2020-09-15
 */
@Controller
@RequestMapping(value = "${adminPath}/test2/farm")
public class FarmController extends BaseController {

	@Autowired
	private FarmService farmService;
	
	@ModelAttribute
	public Farm get(@RequestParam(required=false) String id) {
		Farm entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = farmService.get(id);
		}
		if (entity == null){
			entity = new Farm();
		}
		return entity;
	}
	
	@RequiresPermissions("test2:farm:view")
	@RequestMapping(value = {"list", ""})
	public String list(Farm farm, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Farm> page = farmService.findPage(new Page<Farm>(request, response), farm); 
		model.addAttribute("page", page);
		return "modules/test2/farmList";
	}

	@RequiresPermissions("test2:farm:view")
	@RequestMapping(value = "form")
	public String form(Farm farm, Model model) {
//
//		List<FarmClass> FarmClassList = null;
//
//
//		if(StringUtils.isBlank(farm.getId())){
//
//		}else{
//			FarmClass farmClass =  new FarmClass();
//			farmClass.setFarmId(farm.getId());
//			FarmClassList = farmService.findFarmClassList(farmClass);
//		}
//
//		if (null == FarmClassList || FarmClassList.isEmpty()) {
//			FarmClassList = new ArrayList<>();
//			FarmClassList.add(new FarmClass());
//		}


//
//		model.addAttribute("FarmClassList", FarmClassList);
		model.addAttribute("farm", farm);

		return "modules/test2/farmForm";
	}

	@RequiresPermissions("test2:farm:edit")
	@RequestMapping(value = "save")
	public String save(Farm farm, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, farm)){
			return form(farm, model);
		}
		farmService.save(farm);
		addMessage(redirectAttributes, "保存基本信息成功");
		return "redirect:"+Global.getAdminPath()+"/test2/farm/?repage";
	}
	
	@RequiresPermissions("test2:farm:edit")
	@RequestMapping(value = "delete")
	public String delete(Farm farm, RedirectAttributes redirectAttributes) {
		farmService.delete(farm);
		addMessage(redirectAttributes, "删除基本信息成功");
		return "redirect:"+Global.getAdminPath()+"/test2/farm/?repage";
	}

}